//ok
namespace hjc {

    export type TBarParams = {
        thumb?: string,
        bg?: string,
        thumbGrids?: S9GridsOpitons,
        bgGrids?: S9GridsOpitons,
        thumbDisabled?: string,
        bgDisabled?: string,
        bgPadding?: AlignOptions,
        lbl?: eui.Label,
        text?: string,
    }

    export class ProgressBar extends BaseUI implements ITextural, ITextual {

        protected $bg: string;
        protected $thumb: string;
        protected $bgDisabled: string;
        protected $thumbDisabled: string;
        protected $text: string;
        protected $textDisabled: string;
        protected $textSize: number;
        protected $textColor: number;
        protected $textColorDisabled: number;
        protected $sound: string;
        protected thumbImg: eui.Image;
        protected bgImg: eui.Image;
        protected lbl: eui.Label | null;
        /** 逻辑数据 */
        private $params: TBarParams;            //显示参数
        private $max: number = 100;				//最大值
        private $value: number = 0;				//当前值
        private $lblType: hjc.BarTextType;	    //文字显示类型

        public get bg(): string { return this.$bg ? this.$bg : hjc.Skin.Default.PROGRESSBAR_BG; }
        public set bg(bg: string) { this.$bg = bg; }
        public get thumb(): string { return this.$thumb ? this.$thumb : hjc.Skin.Default.PROGRESSBAR_THUMB; }
        public set thumb(thumb: string) { this.$thumb = thumb; }
        public get bgDisabled(): string { return this.$bgDisabled ? this.$bgDisabled : this.bg; }
        public set bgDisabled(bgDisabled: string) { this.$bgDisabled = bgDisabled; }
        public get thumbDisabled(): string { return this.$thumbDisabled ? this.$thumbDisabled : this.thumb; }
        public set thumbDisabled(thumbDisabled: string) { this.$thumbDisabled = thumbDisabled; }
        public get text(): string { return this.$text; }
        public set text(text: string) { this.$text = text; }
        public get textDisabled(): string { return this.$textDisabled ? this.$textDisabled : this.$text; }
        public set textDisabled(textDisabled: string) { this.$textDisabled = textDisabled; }
        public get textSize(): number { return this.$textSize; }
        public set textSize(size: number) { this.$textSize = size; }
        public get textColor(): number { return this.$textColor; }
        public set textColor(color: number) { this.$textColor = color; }
        public get textColorDisabled(): number { return this.$textColorDisabled ? this.$textColorDisabled : this.$textColor; }
        public set textColorDisabled(colorDisabled: number) { this.$textColorDisabled = colorDisabled; }
        public set lblType(type: hjc.BarTextType) { this.$lblType = type; }

        protected _init(): void {
            this.name = "进度条";
            this.$text = "";
            this.$textSize = Font.Size.MID;
            this.$textColor = Color.Default.TEXT;
            this.touchEnabled = false;
            this.touchChildren = false;
            this.$max = 100;
            this.$value = 0;
            this.$lblType = hjc.BarTextType.RATIO;
        }

        protected _created(): void {
            if (!this.$thumb || this.$thumb === hjc.Skin.Default.PROGRESSBAR_THUMB) {
                ui.setS9grids(this.thumbImg, hjc.Skin.Default.PROGRESSBAR_THUMB_S9GRIDS);
                ui.setAlign(this.thumbImg, hjc.Skin.Default.PROGRESSBAR_THUMB_PADDING);
            }
            if (!this.$bg || this.$thumb === hjc.Skin.Default.PROGRESSBAR_BG) ui.setS9grids(this.bgImg, hjc.Skin.Default.PROGRESSBAR_BG_S9GRIDS);
            this.validateNow();
        }

        public setAs(params: TBarParams): ProgressBar {
            if (notEmpty(params.thumb)) this.$thumb = params.thumb;
            if (notEmpty(params.bg)) this.$bg = params.bg;
            if (notEmpty(params.thumbGrids)) this.thumbImg.scale9Grid
                = new egret.Rectangle(params.thumbGrids.x, params.thumbGrids.y, params.thumbGrids.w, params.thumbGrids.h);
            if (notEmpty(params.bgGrids)) this.bgImg.scale9Grid
                = new egret.Rectangle(params.bgGrids.x, params.bgGrids.y, params.bgGrids.w, params.bgGrids.h);
            if (notEmpty(params.thumbDisabled)) this.$thumbDisabled = params.thumbDisabled;
            if (notEmpty(params.bgDisabled)) this.$bgDisabled = params.bgDisabled;
            if (notEmpty(params.bgPadding)) ui.setAlign(this.thumbImg, params.bgPadding);
            if (params.lbl) this.setLbl(params.lbl);
            if (params.text) this.setText(params.text);
            this._update();
            return this;
        }

        public setImgs(thumb: string, bg?: string): ProgressBar {
            if (notEmpty(thumb)) this.$thumb = thumb;
            if (notEmpty(bg)) this.$bg = bg;
            return this;
        }

        public setLbl(lbl: eui.Label | null): ProgressBar {
            if (this.lbl && !lbl) this.lbl.visible = false;
            else {
                this.lbl = lbl;
                this.lbl.visible = true;
            }
            return this;
        }

        public getLbl(): eui.Label {
            return this.lbl;
        }

        public setValue(value: number, max?: number): ProgressBar {
            if (Math.isPositive(max)) this.$max = max;
            this.$value = utils.num(value, 0, 0, this.$max);
            return this._update();
        }

        private _update(): ProgressBar {
            effect.toShowPart(this.thumbImg, { start: 0, end: this.$value / this.$max, direction: eui.Direction.LTR });
            if (this.lbl) {
                if (this.$lblType === hjc.BarTextType.RATIO) this.lbl.text = `${this.$value}/${this.$max}`;
                else if (this.$lblType === hjc.BarTextType.PERCENT) this.lbl.text = `${Math.round(this.$value / this.$max) * 100}%`;
                else if (this.$lblType === hjc.BarTextType.CUR_VALUE) this.lbl.text = `${Math.round(this.$value)}`;
            }
            return this;
        }

        public setText(text: string, params?: TText): ProgressBar {
            this.$text = text;
            this.lbl.text = text;
            if (notEmpty(params)) ui.setText(this.lbl, params);
            return this;
        }

        public setTextVertial(vertical: boolean): ProgressBar {
            this.lbl.multiline = vertical ? false : true;
            this.lbl.maxWidth = vertical ? this.width : null;
            return this;
        }
    }
}